home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 3: Developer Tools / Linux Cubed Series 3 - Developer Tools.iso / devel / lang / fortran / f2c-9510.000 / f2c-9510 / f2c-951007-libs-1.1 / src / niceprintf.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-10-07  |  10.5 KB  |  442 lines

  1. /****************************************************************
  2. Copyright 1990, 1991, 1993, 1994 by AT&T Bell Laboratories and Bellcore.
  3.  
  4. Permission to use, copy, modify, and distribute this software
  5. and its documentation for any purpose and without fee is hereby
  6. granted, provided that the above copyright notice appear in all
  7. copies and that both that the copyright notice and this
  8. permission notice and warranty disclaimer appear in supporting
  9. documentation, and that the names of AT&T Bell Laboratories or
  10. Bellcore or any of their entities not be used in advertising or
  11. publicity pertaining to distribution of the software without
  12. specific, written prior permission.
  13.  
  14. AT&T and Bellcore disclaim all warranties with regard to this
  15. software, including all implied warranties of merchantability
  16. and fitness.  In no event shall AT&T or Bellcore be liable for
  17. any special, indirect or consequential damages or any damages
  18. whatsoever resulting from loss of use, data or profits, whether
  19. in an action of contract, negligence or other tortious action,
  20. arising out of or in connection with the use or performance of
  21. this software.
  22. ****************************************************************/
  23.  
  24. #include "defs.h"
  25. #include "names.h"
  26. #include "output.h"
  27. #ifndef KR_headers
  28. #include "stdarg.h"
  29. #endif
  30.  
  31. #define TOO_LONG_INDENT (2 * tab_size)
  32. #define MAX_INDENT 44
  33. #define MIN_INDENT 22
  34. static int last_was_newline = 0;
  35. int sharp_line = 0;
  36. int indent = 0;
  37. int in_comment = 0;
  38. int in_define = 0;
  39.  extern int gflag1;
  40.  extern char filename[];
  41.  
  42.  static void ind_printf Argdcl((int, FILE*, char*, va_list));
  43.  
  44.  static void
  45. #ifdef KR_headers
  46. write_indent(fp, use_indent, extra_indent, start, end)
  47.     FILE *fp;
  48.     int use_indent;
  49.     int extra_indent;
  50.     char *start;
  51.     char *end;
  52. #else
  53. write_indent(FILE *fp, int use_indent, int extra_indent, char *start, char *end)
  54. #endif
  55. {
  56.     int ind, tab;
  57.  
  58.     if (sharp_line) {
  59.     fprintf(fp, "#line %ld \"%s\"\n", lineno, filename);
  60.     sharp_line = 0;
  61.     }
  62.     if (in_define == 1) {
  63.     in_define = 2;
  64.     use_indent = 0;
  65.     }
  66.     if (last_was_newline && use_indent) {
  67.     if (*start == '\n') do {
  68.         putc('\n', fp);
  69.         if (++start > end)
  70.             return;
  71.         }
  72.         while(*start == '\n');
  73.  
  74.     ind = indent <= MAX_INDENT
  75.         ? indent
  76.         : MIN_INDENT + indent % (MAX_INDENT - MIN_INDENT);
  77.  
  78.     tab = ind + extra_indent;
  79.  
  80.     while (tab > 7) {
  81.         putc ('\t', fp);
  82.         tab -= 8;
  83.     } /* while */
  84.  
  85.     while (tab-- > 0)
  86.         putc (' ', fp);
  87.     } /* if last_was_newline */
  88.  
  89.     while (start <= end)
  90.     putc (*start++, fp);
  91. } /* write_indent */
  92.  
  93. #ifdef KR_headers
  94. /*VARARGS2*/
  95.   void
  96.  margin_printf (fp, a, b, c, d, e, f, g)
  97.   FILE *fp;
  98.   char *a;
  99.   long b, c, d, e, f, g;
  100. {
  101.     ind_printf (0, fp, a, b, c, d, e, f, g);
  102. } /* margin_printf */
  103.  
  104. /*VARARGS2*/
  105.   void
  106.  nice_printf (fp, a, b, c, d, e, f, g)
  107.   FILE *fp;
  108.   char *a;
  109.   long b, c, d, e, f, g;
  110. {
  111.     ind_printf (1, fp, a, b, c, d, e, f, g);
  112. } /* nice_printf */
  113. #define SPRINTF(x,a,b,c,d,e,f,g) sprintf(x,a,b,c,d,e,f,g)
  114.  
  115. #else /* if (!defined(KR_HEADERS)) */
  116.  
  117. #define SPRINTF(x,a,b,c,d,e,f,g) vsprintf(x,a,ap)
  118.  
  119.   void
  120.  margin_printf(FILE *fp, char *fmt, ...)
  121. {
  122.     va_list ap;
  123.     va_start(ap,fmt);
  124.     ind_printf(0, fp, fmt, ap);
  125.     va_end(ap);
  126.     }
  127.  
  128.   void
  129.  nice_printf(FILE *fp, char *fmt, ...)
  130. {
  131.     va_list ap;
  132.     va_start(ap,fmt);
  133.     ind_printf(1, fp, fmt, ap);
  134.     va_end(ap);
  135.     }
  136. #endif
  137.  
  138. #define  max_line_len c_output_line_length
  139.          /* 74Number of characters allowed on an output
  140.                        line.  This assumes newlines are handled
  141.                        nicely, i.e. a newline after a full text
  142.                        line on a terminal is ignored */
  143.  
  144. /* output_buf   holds the text of the next line to be printed.  It gets
  145.    flushed when a newline is printed.   next_slot   points to the next
  146.    available location in the output buffer, i.e. where the next call to
  147.    nice_printf will have its output stored */
  148.  
  149. static char *output_buf;
  150. static char *next_slot;
  151. static char *string_start;
  152.  
  153. static char *word_start = NULL;
  154. static int cursor_pos = 0;
  155. static int In_string = 0;
  156.  
  157.  void
  158. np_init(Void)
  159. {
  160.     next_slot = output_buf = Alloc(MAX_OUTPUT_SIZE);
  161.     memset(output_buf, 0, MAX_OUTPUT_SIZE);
  162.     }
  163.  
  164.  static char *
  165. #ifdef KR_headers
  166. adjust_pointer_in_string(pointer)
  167.     register char *pointer;
  168. #else
  169. adjust_pointer_in_string(register char *pointer)
  170. #endif
  171. {
  172.     register char *s, *s1, *se, *s0;
  173.  
  174.     /* arrange not to break \002 */
  175.     s1 = string_start ? string_start : output_buf;
  176.     for(s = s1; s < pointer; s++) {
  177.         s0 = s1;
  178.         s1 = s;
  179.         if (*s == '\\') {
  180.             se = s++ + 4;
  181.             if (se > pointer)
  182.                 break;
  183.             if (*s < '0' || *s > '7')
  184.                 continue;
  185.             while(++s < se)
  186.                 if (*s < '0' || *s > '7')
  187.                     break;
  188.             --s;
  189.             }
  190.         }
  191.     return s0 - 1;
  192.     }
  193.  
  194. /* ANSI says strcpy's behavior is undefined for overlapping args,
  195.  * so we roll our own fwd_strcpy: */
  196.  
  197.  static void
  198. #ifdef KR_headers
  199. fwd_strcpy(t, s)
  200.     register char *t;
  201.     register char *s;
  202. #else
  203. fwd_strcpy(register char *t, register char *s)
  204. #endif
  205. { while(*t++ = *s++); }
  206.  
  207. /* isident -- true iff character could belong to a unit.  C allows
  208.    letters, numbers and underscores in identifiers.  This also doubles as
  209.    a check for numeric constants, since we include the decimal point and
  210.    minus sign.  The minus has to be here, since the constant "10e-2"
  211.    cannot be broken up.  The '.' also prevents structure references from
  212.    being broken, which is a quite acceptable side effect */
  213.  
  214. #define isident(x) (Tr[x] & 1)
  215. #define isntident(x) (!Tr[x])
  216.  
  217.   static void
  218. #ifdef KR_headers
  219.  ind_printf (use_indent, fp, a, b, c, d, e, f, g)
  220.   int use_indent;
  221.   FILE *fp;
  222.   char *a;
  223.   long b, c, d, e, f, g;
  224. #else
  225.  ind_printf (int use_indent, FILE *fp, char *a, va_list ap)
  226. #endif
  227. {
  228.     extern int max_line_len;
  229.     extern FILEP c_file;
  230.     extern char tr_tab[];    /* in output.c */
  231.     register char *Tr = tr_tab;
  232.     int ch, inc, ind;
  233.     static int extra_indent, last_indent, set_cursor = 1;
  234.  
  235.     cursor_pos += indent - last_indent;
  236.     last_indent = indent;
  237.     SPRINTF (next_slot, a, b, c, d, e, f, g);
  238.  
  239.     if (fp != c_file) {
  240.     fprintf (fp,"%s", next_slot);
  241.     return;
  242.     } /* if fp != c_file */
  243.  
  244.     do {
  245.     char *pointer;
  246.  
  247. /* The   for   loop will parse one output line */
  248.  
  249.     if (set_cursor) {
  250.         ind = indent <= MAX_INDENT
  251.             ? indent
  252.             : MIN_INDENT + indent % (MAX_INDENT - MIN_INDENT);
  253.         cursor_pos = ind + extra_indent;
  254.         set_cursor = 0;
  255.         }
  256.     if (in_comment)
  257.             for (pointer = next_slot; *pointer && *pointer != '\n' &&
  258.                 cursor_pos <= max_line_len; pointer++)
  259.             cursor_pos++;
  260.     else
  261.           for (pointer = next_slot; *pointer && *pointer != '\n' &&
  262.         cursor_pos <= max_line_len; pointer++) {
  263.  
  264.         /* Update state variables here */
  265.  
  266.         if (In_string) {
  267.         switch(*pointer) {
  268.             case '\\':
  269.                 if (++cursor_pos > max_line_len) {
  270.                     cursor_pos -= 2;
  271.                     --pointer;
  272.                     goto overflow;
  273.                     }
  274.                 ++pointer;
  275.                 break;
  276.             case '"':
  277.                 In_string = 0;
  278.                 word_start = 0;
  279.             }
  280.         }
  281.         else switch (*pointer) {
  282.             case '"':
  283.             if (cursor_pos + 5 > max_line_len) {
  284.                 word_start = 0;
  285.                 --pointer;
  286.                 goto overflow;
  287.                 }
  288.             In_string = 1;
  289.             string_start = word_start = pointer;
  290.                 break;
  291.             case '\'':
  292.             if (pointer[1] == '\\')
  293.                 if ((ch = pointer[2]) >= '0' && ch <= '7')
  294.                     for(inc = 3; pointer[inc] != '\''
  295.                         && ++inc < 5;);
  296.                 else
  297.                     inc = 3;
  298.             else
  299.                 inc = 2;
  300.             /*debug*/ if (pointer[inc] != '\'')
  301.             /*debug*/  fatalstr("Bad character constant %.10s",
  302.                     pointer);
  303.             if ((cursor_pos += inc) > max_line_len) {
  304.                 cursor_pos -= inc;
  305.                 word_start = 0;
  306.                 --pointer;
  307.                 goto overflow;
  308.                 }
  309.             word_start = pointer;
  310.             pointer += inc;
  311.             break;
  312.         case '\t':
  313.             cursor_pos = 8 * ((cursor_pos + 8) / 8) - 1;
  314.             break;
  315.         default: {
  316.  
  317. /* HACK  Assumes that all characters in an atomic C token will be written
  318.    at the same time.  Must check for tokens first, since '-' is considered
  319.    part of an identifier; checking isident first would mean breaking up "->" */
  320.  
  321.             if (word_start) {
  322.             if (isntident(*(unsigned char *)pointer))
  323.                 word_start = NULL;
  324.             }
  325.             else if (isident(*(unsigned char *)pointer))
  326.             word_start = pointer;
  327.             break;
  328.         } /* default */
  329.         } /* switch */
  330.         cursor_pos++;
  331.     } /* for pointer = next_slot */
  332.  overflow:
  333.     if (*pointer == '\0') {
  334.  
  335. /* The output line is not complete, so break out and don't output
  336.    anything.  The current line fragment will be stored in the buffer */
  337.  
  338.         next_slot = pointer;
  339.         break;
  340.     } else {
  341.         char last_char;
  342.         int in_string0 = In_string;
  343.  
  344. /* If the line was too long, move   pointer   back to the character before
  345.    the current word.  This allows line breaking on word boundaries.  Make
  346.    sure that 80 character comment lines get broken up somehow.  We assume
  347.    that any non-string 80 character identifier must be in a comment.
  348. */
  349.  
  350.         if (*pointer == '\n')
  351.         in_define = 0;
  352.         else if (word_start && word_start > output_buf)
  353.         if (In_string)
  354.             if (string_start && pointer - string_start < 5)
  355.                 pointer = string_start - 1;
  356.             else {
  357.                 pointer = adjust_pointer_in_string(pointer);
  358.                 string_start = 0;
  359.                 }
  360.         else if (word_start == string_start
  361.                 && pointer - string_start >= 5) {
  362.             pointer = adjust_pointer_in_string(next_slot);
  363.             In_string = 1;
  364.             string_start = 0;
  365.             }
  366.         else
  367.             pointer = word_start - 1;
  368.         else if (cursor_pos > max_line_len) {
  369. #ifndef ANSI_Libraries
  370.         extern char *strchr();
  371. #endif
  372.         if (In_string) {
  373.             pointer = adjust_pointer_in_string(pointer);
  374.             if (string_start && pointer > string_start)
  375.                 string_start = 0;
  376.             }
  377.         else if (strchr("&*+-/<=>|", *pointer)
  378.             && strchr("!%&*+-/<=>^|", pointer[-1])) {
  379.             pointer -= 2;
  380.             if (strchr("<>", *pointer)) /* <<=, >>= */
  381.                 pointer--;
  382.             }
  383.         else {
  384.             if (word_start)
  385.                 while(isident(*(unsigned char *)pointer))
  386.                     pointer++;
  387.             pointer--;
  388.             }
  389.         }
  390.         last_char = *pointer;
  391.         write_indent(fp, use_indent, extra_indent, output_buf, pointer);
  392.         next_slot = output_buf;
  393.         if (In_string && !string_start && Ansi == 1 && last_char != '\n')
  394.         *next_slot++ = '"';
  395.         fwd_strcpy(next_slot, pointer + 1);
  396.  
  397. /* insert a line break */
  398.  
  399.         if (last_char == '\n') {
  400.         if (In_string)
  401.             last_was_newline = 0;
  402.         else {
  403.             last_was_newline = 1;
  404.             extra_indent = 0;
  405.             sharp_line = gflag1;
  406.             }
  407.         }
  408.         else {
  409.         extra_indent = TOO_LONG_INDENT;
  410.         if (In_string && !string_start) {
  411.             if (Ansi == 1) {
  412.                 fprintf(fp, gflag1 ? "\"\\\n" : "\"\n");
  413.                 use_indent = 1;
  414.                 last_was_newline = 1;
  415.                 }
  416.             else {
  417.                 fprintf(fp, "\\\n");
  418.                 last_was_newline = 0;
  419.                 }
  420.             In_string = in_string0;
  421.             }
  422.         else {
  423.             if (in_define/* | gflag1*/)
  424.                 putc('\\', fp);
  425.             putc ('\n', fp);
  426.             last_was_newline = 1;
  427.             }
  428.         } /* if *pointer != '\n' */
  429.  
  430.         if (In_string && Ansi != 1 && !string_start)
  431.         cursor_pos = 0;
  432.         else
  433.         set_cursor = 1;
  434.  
  435.         string_start = word_start = NULL;
  436.  
  437.     } /* else */
  438.  
  439.     } while (*next_slot);
  440.  
  441. } /* ind_printf */
  442.